#include "stm32f10x.h"
#include <string.h>
#include "freertos.h"
#include "task.h"
#include "queue.h"
#include "semphr.h"
#include "UART1.h"
#include "LED.h"
#include "DELAY.h"
#include "string.h"
#include "ADC.h"

TaskHandle_t hand1;
QueueHandle_t queue, queue2, queue3;

#define BUF_SIZE 200
static uint8_t g_BufferIndex = 0;
static char g_Buffer[BUF_SIZE] = {0};

//从串口1接收账号密码+联网第一条AT指令
void recv_pass(void);

//从串口2读取ESP01S回复数据+构建g_Buffer
void read_queue(void);

//判断是否有OK,点灯
void if_ok(void);

//继续发送AT指令，连接阿里云
void aliyun(void);

void task1(void * arg) {
    // -------------从串口1接收账号密码+联网第一条AT指令---------------
    recv_pass();

    //--------------从串口2读取ESP01S回复数据+构建g_Buffer---------------
    //read_queue();

    //    //---------------------判断g_Buffer中有没有"ok"+点灯-------------------
    if_ok();

    //    //---------------------发送后续AT指令--------------------------
    aliyun();


    while (1) {
    }

}

//从串口1接收账号密码+联网第一条AT指令
void recv_pass(void) {
    printf3("77\r\n");

    int times = 0;
    char buf[50] = {0};

    while (1) {
        char ch = 0;
        BaseType_t ret = xQueueReceive(queue, &ch, 1000);

        //判断队列是否接收完成蓝牙传来的名字和密码!...=...!
        if (ret == pdTRUE) {
            buf[strlen(buf)] = ch;

            if (ch == '!') {
                times ++;
            }

            if (times == 2) {
                break;
            }
        }

    }


    printf3("102\r\n");

    // ---------------收完账号密码： 切割----------------
    char buf1[25] = {0};
    char buf2[25] = {0};

    for (int i = 1; i < 50 ; i++) {
        if (buf[i] != '=') { //拆分名字字符串
            buf1[i - 1] = buf[i];
        }

        if (buf[i] == '=') { //拆分密码字符串
            int j = i + 1;
            int idx = 0;

            for (; j < strlen(buf) -1; j++) {
                buf2[idx] = buf[j];
                idx++;
            }
        }
    }

    printf3("%s,%s\r\n", buf1, buf2);
    printf2("AT+CWJAP_DEF=\"%s\",\"%s\"\r\n", buf1, buf2);	//传给esp
    read_queue();

    printf3("send AT1\r\n");

    memset(buf, 0, strlen(buf));//释放buf内存空间
    memset(buf1, 0, strlen(buf1));//释放buf1内存空间
    memset(buf2, 0, strlen(buf2));//释放buf2内存空间
}



//从串口2读取ESP01S回复数据+构建g_Buffer
void read_queue(void) {
    //printf3("read 2 before\r\n");

    char ch = 0;

    while (xQueueReceive(queue2, &ch, 4000) == pdTRUE) {

        printf3("%c", ch);
        g_Buffer[g_BufferIndex++] = ch;
    }

    //printf3("read 2 after\r\n");
}



//判断是否有OK,点灯
void if_ok(void) {
    if (strstr(g_Buffer, "OK")) {
        printf3("yes\r\n");
        Turn_On_LED();
    } else {
        printf3("no\r\n");
        Turn_Off_LED();
    }

    memset(g_Buffer, 0, sizeof(g_Buffer));
    g_BufferIndex = 0;
}


//继续发送AT指令，连接阿里云
void aliyun(void) {
    vTaskDelay(3000);
    printf2("AT+CIPSTART=\"TCP\",\"47.115.220.165\",9053\r\n");

    read_queue();

    if (strstr(g_Buffer, "OK")) {
        printf3("yes\r\n");
        Turn_On_LED2();
    } else {
        printf3("no\r\n");
        Turn_Off_LED2();
    }

    memset(g_Buffer, 0, sizeof(g_Buffer));
    g_BufferIndex = 0;

    //vTaskDelay(1000);

    printf2("AT+CIPMODE=1\r\n");
    read_queue();
    memset(g_Buffer, 0, sizeof(g_Buffer));
    g_BufferIndex = 0;

    vTaskDelay(3000);

    printf2("AT+CIPSEND\r\n");
    read_queue();
    memset(g_Buffer, 0, sizeof(g_Buffer));
    g_BufferIndex = 0;

    vTaskDelay(3000);

    //读传感器数据

    char send_data[100] = {0};

    int times = 0;

    while (1) {
        getMSG(send_data);
        times++;
        printf3("%s\r\n,", send_data);
        printf2("POST /0 HTTP/1.1 \r\n\r\n%s;", send_data);
        vTaskDelay(1000);

        if (times == 10) {
            break;
        }
    }

    read_queue();
    memset(g_Buffer, 0, sizeof(g_Buffer));
    g_BufferIndex = 0;


    vTaskDelay(3000);

    printf2("+++");
    read_queue();
    memset(g_Buffer, 0, sizeof(g_Buffer));
    g_BufferIndex = 0;

    vTaskDelay(3000);

    printf2("AT+CIPMODE=0\r\n");
    read_queue();
    memset(g_Buffer, 0, sizeof(g_Buffer));
    g_BufferIndex = 0;

    //vTaskDelay(1000);

    printf2("AT+CIPCLOSE\r\n");


    read_queue();
    //vTaskDelay(3000);
    memset(g_Buffer, 0, sizeof(g_Buffer));
    g_BufferIndex = 0;

}

int main(void) {
    NVIC_PriorityGroupConfig(NVIC_PriorityGroup_4);
    USART1_Init();
    USART2_Init();
    USART3_Init();
    Delay_Init();
    LED_Init();
    AD_Init();

    //vTaskDelay(5000);

    queue = xQueueCreate(40, 1);
    queue2 = xQueueCreate(40, 1);
    queue3 = xQueueCreate(40, 1);


    xTaskCreate(task1, "Task1", configMINIMAL_STACK_SIZE * 4, NULL, tskIDLE_PRIORITY + 1, &hand1);

    vTaskStartScheduler();

    while (1) {


    }
}
